using BarcodePrinting;
using Microsoft.Office.Interop.Excel;
using OrderSystem.SystemFramework;
using System;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationLabel
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationLabel(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeLabelDataToExcel(string templetFile, LabelModel[] labels)
		{
			this.OpenExcel(templetFile);
			string defaultVersionOfOutType = AppSettings.GetDefaultVersionOfOutType("label");
			if (defaultVersionOfOutType == "1")
			{
				this.MakeLabelDataToExcel_1(labels);
			}
			if (defaultVersionOfOutType == "2")
			{
				this.MakeLabelDataToExcel_2(labels);
			}
			if (defaultVersionOfOutType == "3")
			{
				this.MakeLabelDataToExcel_3(labels);
			}
			if (defaultVersionOfOutType == "4")
			{
				this.MakeLabelDataToExcel_4(labels);
			}
			if (defaultVersionOfOutType == "5")
			{
				this.MakeLabelDataToExcel_5(labels);
			}
			if (defaultVersionOfOutType == "6")
			{
				this.MakeLabelDataToExcel_6(labels);
			}
			if (defaultVersionOfOutType == "7")
			{
				this.MakeLabelDataToExcel_7(labels);
			}
			if (defaultVersionOfOutType == "8")
			{
				this.MakeLabelDataToExcel_8(labels);
			}
			if (defaultVersionOfOutType == "9")
			{
				this.MakeLabelDataToExcel_9(labels);
			}
		}

		private void MakeLabelDataToExcel_5(LabelModel[] labels)
		{
			int num = 40;
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].Quantity * labels[i].Multiple;
			}
			int num4 = num2 % num;
			int num5;
			if (num4 == 0)
			{
				num5 = num2 / num;
			}
			else
			{
				num5 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num5 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num5; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num6 = 1;
				int num7 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num8 = labels[i].Quantity * labels[i].Multiple;
					for (int j = 0; j < num8; j++)
					{
						if (j % labels[i].Multiple == 0)
						{
							num3++;
						}
						int num9 = (num6 - 1) * 6 + 1;
						int num10 = (num7 - 1) * 3 + 2;
						this.workSheet.Cells[num9, num10]= labels[i].ProjectName;
						this.workSheet.Cells[num9 + 1, num10]= labels[i].ProductType;
						this.workSheet.Cells[num9 + 2, num10]= labels[i].ProductCode;
						this.workSheet.Cells[num9 + 3, num10]= labels[i].Width.ToString() + "*" + labels[i].Hight.ToString();
						this.workSheet.Cells[num9 + 4, num10]= labels[i].ProductID;
						num7++;
						if (num7 > 4)
						{
							num6++;
							num7 = 1;
						}
						if (num6 > 10)
						{
							if (i < labels.Length - 1 || j < num8 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num7 = 1;
								num6 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_4(LabelModel[] labels)
		{
			int num = 38;
			int num2 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].Quantity * labels[i].Multiple;
			}
			int num3 = num2 % num;
			int num4;
			if (num3 == 0)
			{
				num4 = num2 / num;
			}
			else
			{
				num4 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num4 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num4; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num5 = 1;
				int num6 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num7 = labels[i].Quantity * labels[i].Multiple;
					for (int j = 0; j < num7; j++)
					{
						int num8 = num5 + 1;
						int num9 = (num6 - 1) * 5 + 2;
						this.workSheet.Cells[num8, num9]= labels[i].ProjectName;
						this.workSheet.Cells[num8, num9 + 1]= labels[i].ProductCode;
						double num10 = labels[i].Width * labels[i].Hight / 1000000.0;
						this.workSheet.Cells[num8, num9 + 2]= labels[i].Width.ToString() + "*" + labels[i].Hight.ToString();
						this.workSheet.Cells[num8, num9 + 3]= num10.ToString("F2");
						this.workSheet.Cells[num8, num9 + 4]= labels[i].Remark;
						num6++;
						if (num6 > 2)
						{
							num5++;
							num6 = 1;
						}
						if (num5 > 19)
						{
							if (i < labels.Length - 1 || j < num7 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num6 = 1;
								num5 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_3(LabelModel[] labels)
		{
			int num = 50;
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].PrintQuantity * labels[i].PrintMultiple;
			}
			int num4 = num2 % num;
			int num5;
			if (num4 == 0)
			{
				num5 = num2 / num;
			}
			else
			{
				num5 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num5 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num5; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num6 = 1;
				int num7 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num8 = labels[i].PrintQuantity * labels[i].PrintMultiple;
					for (int j = 0; j < num8; j++)
					{
						if (j % labels[i].Multiple == 0)
						{
							num3++;
						}
						int num9 = (num6 - 1) * 6 + 2;
						int num10 = (num7 - 1) * 2 + 2;
						this.workSheet.Cells[num9, num10]= (labels[i].LabelName == "") ? labels[i].CompanyName : labels[i].LabelName;
						this.workSheet.Cells[num9 + 1, num10]= labels[i].ProductType;
						this.workSheet.Cells[num9 + 2, num10]= labels[i].ProductCode + "  " + labels[i].Remark;
						string text = string.Concat(new string[]
						{
							labels[i].Width.ToString(),
							(labels[i].SubtenseOfWidth == "") ? "" : ("/" + labels[i].SubtenseOfWidth),
							"×",
							labels[i].Hight.ToString(),
							(labels[i].SubtenseOfHight == "") ? "" : ("/" + labels[i].SubtenseOfHight)
						});
						this.workSheet.Cells[num9 + 3, num10]= text;
						this.workSheet.Cells[num9 + 4, num10]= labels[i].ProductID;
						num7++;
						if (num7 > 5)
						{
							num6++;
							num7 = 1;
						}
						if (num6 > 10)
						{
							if (i < labels.Length - 1 || j < num8 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num7 = 1;
								num6 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_6(LabelModel[] labels)
		{
			int num = 40;
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].Quantity * labels[i].Multiple;
			}
			int num4 = num2 % num;
			int num5;
			if (num4 == 0)
			{
				num5 = num2 / num;
			}
			else
			{
				num5 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num5 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num5; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num6 = 1;
				int num7 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num8 = labels[i].Quantity * labels[i].Multiple;
					for (int j = 0; j < num8; j++)
					{
						if (j % labels[i].Multiple == 0)
						{
							num3++;
						}
						int num9 = (num6 - 1) * 6 + 2;
						int num10 = (num7 - 1) * 2 + 2;
						this.workSheet.Cells[num9, num10]= labels[i].ProjectName;
						this.workSheet.Cells[num9 + 1, num10]= labels[i].ProductType;
						this.workSheet.Cells[num9 + 2, num10]= labels[i].ProductCode;
						this.workSheet.Cells[num9 + 3, num10]= labels[i].Width.ToString() + "*" + labels[i].Hight.ToString();
						this.workSheet.Cells[num9 + 4, num10]= labels[i].Remark + ((labels[i].Technics != "") ? ("/" + labels[i].Technics) : "");
						this.workSheet.Cells[num9 + 5, num10]= labels[i].ProductID;
						num7++;
						if (num7 > 4)
						{
							num6++;
							num7 = 1;
						}
						if (num6 > 10)
						{
							if (i < labels.Length - 1 || j < num8 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num7 = 1;
								num6 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_1(LabelModel[] labels)
		{
			int num = 34;
			int num2 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].Quantity * labels[i].Multiple;
			}
			int num3 = num2 % num;
			int num4;
			if (num3 == 0)
			{
				num4 = num2 / num;
			}
			else
			{
				num4 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num4 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num4; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num5 = 1;
				int num6 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num7 = labels[i].Quantity * labels[i].Multiple;
					for (int j = 0; j < num7; j++)
					{
						int num8 = num5;
						int num9 = (num6 - 1) * 7 + 1;
						this.workSheet.Cells[num8 * 2 - 1, num9]= labels[i].ProjectName;
						this.workSheet.Cells[num8 * 2 - 1, num9 + 2]= labels[i].ProductCode;
						this.workSheet.Cells[num8 * 2 - 1, num9 + 3]= labels[i].Width.ToString();
						this.workSheet.Cells[num8 * 2 - 1, num9 + 4]= "*";
						this.workSheet.Cells[num8 * 2 - 1, num9 + 5]= labels[i].Hight.ToString();
						this.workSheet.Cells[num8 * 2 - 1, num9 + 6]= labels[i].Remark;
						num6++;
						if (num6 > 2)
						{
							num5++;
							num6 = 1;
						}
						if (num5 > 17)
						{
							if (i < labels.Length - 1 || j < num7 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num5 = 1;
								num6 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_2(LabelModel[] labels)
		{
			int num = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num += labels[i].Quantity * labels[i].Multiple;
			}
			int num2 = num % 38;
			int num3;
			if (num2 == 0)
			{
				num3 = num / 38;
			}
			else
			{
				num3 = num / 38 + 1;
			}
			try
			{
				for (int i = 0; i < num3 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num3; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Tag-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				bool flag = true;
				int num4 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int quantity = labels[i].Quantity;
					for (int j = 0; j < quantity; j++)
					{
						if (flag)
						{
							this.workSheet.Cells[num4, 1]= labels[i].ProjectName;
							this.workSheet.Cells[num4, 2]= labels[i].ProductType;
							this.workSheet.Cells[num4, 3]= labels[i].ProductCode;
							this.workSheet.Cells[num4, 4]= labels[i].Width;
							this.workSheet.Cells[num4, 5]= "*";
							this.workSheet.Cells[num4, 6]= labels[i].Hight;
							this.workSheet.Cells[num4, 7]= labels[i].Remark;
						}
						else
						{
							this.workSheet.Cells[num4, 8]= labels[i].ProjectName;
							this.workSheet.Cells[num4, 9]= labels[i].ProductType;
							this.workSheet.Cells[num4, 10]= labels[i].ProductCode;
							this.workSheet.Cells[num4, 11]= labels[i].Width;
							this.workSheet.Cells[num4, 12]= "*";
							this.workSheet.Cells[num4, 13]= labels[i].Hight;
							this.workSheet.Cells[num4, 14]= labels[i].Remark;
						}
						num4 += 2;
						if (num4 > 37 && flag)
						{
							num4 = 1;
							flag = false;
						}
						if (num4 > 37 && !flag)
						{
							this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
							this.workSheet.Activate();
							num4 = 1;
							flag = true;
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_7(LabelModel[] labels)
		{
			int num = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num += labels[i].Quantity * labels[i].Multiple;
			}
			int num2 = num % 30;
			int num3;
			if (num2 == 0)
			{
				num3 = num / 30;
			}
			else
			{
				num3 = num / 30 + 1;
			}
			try
			{
				for (int i = 0; i < num3 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num3; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Page-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				bool flag = true;
				int num4 = 2;
				for (int i = 0; i < labels.Length; i++)
				{
					int num5 = labels[i].PrintQuantity * labels[i].PrintMultiple;
					for (int j = 0; j < num5; j++)
					{
						if (flag)
						{
							this.workSheet.Cells[num4, 1]= (labels[i].LabelName == "") ? labels[i].CompanyName : labels[i].LabelName;
							this.workSheet.Cells[num4, 2]= labels[i].Width.ToString() + "×" + labels[i].Hight;
							this.workSheet.Cells[num4, 3]= labels[i].ProductCode + "/" + labels[i].Remark;
						}
						else
						{
							this.workSheet.Cells[num4, 5]= (labels[i].LabelName == "") ? labels[i].CompanyName : labels[i].LabelName;
							this.workSheet.Cells[num4, 6]= labels[i].Width.ToString() + "×" + labels[i].Hight;
							this.workSheet.Cells[num4, 7]= labels[i].ProductCode + "/" + labels[i].Remark;
						}
						num4++;
						if (num4 > 16 && flag)
						{
							num4 = 2;
							flag = false;
						}
						if (num4 > 16 && !flag)
						{
							this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
							this.workSheet.Activate();
							num4 = 2;
							flag = true;
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_8(LabelModel[] labels)
		{
			int num = 40;
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].Quantity * labels[i].Multiple;
			}
			int num4 = num2 % num;
			int num5;
			if (num4 == 0)
			{
				num5 = num2 / num;
			}
			else
			{
				num5 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num5 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num5; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num6 = 1;
				int num7 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num8 = labels[i].Quantity * labels[i].Multiple;
					for (int j = 0; j < num8; j++)
					{
						if (j % labels[i].Multiple == 0)
						{
							num3++;
						}
						int num9 = (num6 - 1) * 6 + 2;
						int num10 = (num7 - 1) * 3 + 2;
						this.workSheet.Cells[num9, num10]= labels[i].ProjectName;
						this.workSheet.Cells[num9 + 1, num10]= labels[i].ProductType;
						string str = string.Concat(new object[]
						{
							labels[i].Width.ToString(),
							(labels[i].SubtenseOfWidth.ToString() == "") ? "" : ("/" + labels[i].SubtenseOfWidth.ToString()),
							"×",
							labels[i].Hight.ToString(),
							(labels[i].SubtenseOfHight.ToString() == "") ? "" : ("/" + labels[i].SubtenseOfHight.ToString()),
							" =",
							labels[i].Quantity
						});
						this.workSheet.Cells[num9 + 3, num10]= labels[i].ProductCode + "  " + str;
						this.workSheet.Cells[num9 + 4, num10]= labels[i].Remark + ((labels[i].Technics != "") ? ("/" + labels[i].Technics) : "");
						this.workSheet.Cells[num9 + 5, num10]= labels[i].ProductID;
						num7++;
						if (num7 > 4)
						{
							num6++;
							num7 = 1;
						}
						if (num6 > 10)
						{
							if (i < labels.Length - 1 || j < num8 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num7 = 1;
								num6 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeLabelDataToExcel_9(LabelModel[] labels)
		{
			int num = 30;
			int num2 = 0;
			for (int i = 0; i < labels.Length; i++)
			{
				num2 += labels[i].Quantity * labels[i].Multiple;
			}
			int num3 = num2 % num;
			int num4;
			if (num3 == 0)
			{
				num4 = num2 / num;
			}
			else
			{
				num4 = num2 / num + 1;
			}
			try
			{
				for (int i = 0; i < num4 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num4; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Label-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num5 = 1;
				int num6 = 1;
				for (int i = 0; i < labels.Length; i++)
				{
					int num7 = labels[i].Quantity * labels[i].Multiple;
					for (int j = 0; j < num7; j++)
					{
						int num8 = (num6 - 1) * 2 + 2;
						int num9 = (num5 - 1) * 3 + 2;
						this.workSheet.Cells[num9, num8]= string.Concat(new string[]
						{
							labels[i].CompanyName,
							" ",
							labels[i].ProductType,
							labels[i].Width.ToString(),
							"*",
							labels[i].Hight.ToString()
						});
						this.workSheet.Cells[num9 + 1, num8]= labels[i].ProductCode + " " + labels[i].Remark;
						num6++;
						if (num6 > 2)
						{
							num5++;
							num6 = 1;
						}
						if (num5 > 15)
						{
							if (i < labels.Length - 1 || j < num7 - 1)
							{
								this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
								this.workSheet.Activate();
								num5 = 1;
								num6 = 1;
							}
						}
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
